# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=hunspell
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"

# Please rebuild enchant when updating this if needed, otherwise it may
# silently break spell checking in applications
pkgver=1.4.1
pkgrel=1

pkgdesc="Spell checker and morphological analyzer library and program (mingw-w64)"
arch=('any')
url="http://hunspell.github.io/"
# We omit MPL because it cannot be applied to the whole source, see COPYING
license=(GPL2+ LGPL2.1+)

makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=(${MINGW_PACKAGE_PREFIX}-gcc-libs
         ${MINGW_PACKAGE_PREFIX}-gettext)
options=('!libtool' 'strip' '!debug')
optdepends=('perl: for ispellaff2myspell')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/hunspell/hunspell/archive/v${pkgver}.tar.gz"
        0001-canonicalhost.patch
        0002-exit-on-MAX_PREFIXES_SUFFIXES.patch
        0003-inc-MAX_PREFIXES_SUFFIXES-to-2048.patch
        0004-relocate.patch
        0005-windows-sub-dicts-paths.patch
        0006-Fix-for-tool-Munch-when-compiling-with-Mingw64-for-6.patch
        0007-Added-header-stddef-to-munch-and-unmunch.patch)
sha256sums=('c4476aff0ced52eec334eae1e8d3fdaaebdd90f5ecd0b57cf2a92a6fd220d1bb'
            '9bc763d998ffc0ba62130269a24ecec31bd3fa6d648aff7348e21b0401592801'
            '6d526286917ccd98c2ddb21f603c3cc72e22bf891f42ea7217d25c9ef5244347'
            'd9b6b415733edd512e0bfeb36493e5d344c54b7523d277df49e45ec9b993e9c9'
            '9e21761ec9600b34654f38c0e6c688170026a5b192ea244ea36fdf409c77ad09'
            '057a87ece38e4426b3f71559e935325085bb9f8556050e78125875de231d63b2'
            'ca638305f91eb4e0916edd2adf5e39ce9cd1c09ad0b164302faaaa40f3649471'
            '0edfa83ec56dca544550544192113768be60d5d379c527d3d3b47948c2f98220')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  #remove files created by patch
  rm -rf src/tools/pathtools.cxx src/tools/pathtools.hxx
  patch -Np1 -i "${srcdir}"/0001-canonicalhost.patch
  patch -Np1 -i "${srcdir}"/0002-exit-on-MAX_PREFIXES_SUFFIXES.patch
  patch -Np1 -i "${srcdir}"/0003-inc-MAX_PREFIXES_SUFFIXES-to-2048.patch
  patch -Np1 -i "${srcdir}"/0004-relocate.patch
  patch -Np1 -i "${srcdir}"/0005-windows-sub-dicts-paths.patch

  # See https://github.com/hunspell/hunspell/pull/389
  patch -Np1 -i "${srcdir}"/0006-Fix-for-tool-Munch-when-compiling-with-Mingw64-for-6.patch
  patch -Np1 -i "${srcdir}"/0007-Added-header-stddef-to-munch-and-unmunch.patch

  autoreconf -fi
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  ../${_realname}-${pkgver}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --disable-rpath \
    --enable-threads=posix \
    --without-ui \
    --without-readline
  # temporarily copy hunvisapi.h to work around build process
  # where the file is not spotted.
  cp ${srcdir}/build-${MINGW_CHOST}/src/hunspell/hunvisapi.h \
       "${srcdir}/${_realname}-${pkgver}/src/hunspell"
  MSYS2_ARG_CONV_EXCL="-DBINDIR=;-DDATA_DIR=" \
  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  MSYS2_ARG_CONV_EXCL="-DBINDIR=;-DDATA_DIR=" \
  make DESTDIR="${pkgdir}" install
  cp ${pkgdir}${MINGW_PREFIX}/lib/lib${_realname}-${pkgver%.*}.dll.a ${pkgdir}${MINGW_PREFIX}/lib/lib${_realname}.dll.a

  # Licenses
  cd "${srcdir}/${_realname}-${pkgver}"
  install -Dm644 COPYING      "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 COPYING.LGPL "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LGPL"
  install -Dm644 COPYING.MPL  "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.MPL"
  #remove copy we made earlier.  configure will create the file from
  #/hunspell/hunvisapi.h.in.
#  rm -rf "${srcdir}/${_realname}-${pkgver}/src/hunspell/hunvisapi.h"
}
